import * as React from 'react'
import ReactDOM from 'react-dom'
import classNames from 'classnames'
import TriggerContext from './context'
import Portal from '../util/portal'

const ALL_HANDLERS = [
  'onClick',
  'onMouseDown',
  'onTouchStart',
  'onMouseEnter',
  'onMouseLeave',
  'onFocus',
  'onBlur',
  'onContextMenu'
]

export function generateTrigger(
  ProtalComponent
) {
  class Trigger extends React.Component {
    static contextType = TriggerContext
    static defaultProps = {
      prefixCls: 'addon-layui-trigger-popup',
      popupClassName: '',
      mouseEnterDelay: 0,
      mouseLeaveDelay: 0.1,
      focuseDelay: 0,
      blurDelay: 0.15,
      popupStyle: {},
      destroyPopupOnHide: false,
      popupAlign: {},
      defaultPopupVisible: false,
      mask: false,
      maskCloseable: true,
      action: [],
      showAction: [],
      hideAction: [],
      autoDestory: false
    }

    popupRef = React.createRef()
    triggerRef = React.createRef()

    constructor(props) {
      super(props)
      let popupVisible
      if ('popupVisible' in props) {
        popupVisible = !!props.popupVisible
      } else {
        popupVisible = !!props.defaultPopupVisible
      }

      this.state = {
        prevPopupVisible: popupVisible,
        popupVisible
      }

      ALL_HANDLERS.forEach(handler => {
        this[`fire${handler}`] = e => {
          this.fireEvents(handler, e)
        }
      })
    }

    componentDidMount() {
      this.componentDidUpdate()
    }

    componentDidUpdate() {
      if (this.state.popupVisible) {
        let currentDocument;
        // if (!this.clik)
      }
    }
  }

  return Trigger
}

export default generateTrigger(Protal)